var __assign =
  (this && this.__assign) ||
  function () {
    __assign =
      Object.assign ||
      function (t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
          s = arguments[i];
          for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
        }
        return t;
      };
    return __assign.apply(this, arguments);
  };
import { Button, Form, Input, Modal } from 'antd';
import React, { useRef, useState } from 'react';
import { useConfigContext } from '../../contexts/ConfigContext';
var layout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 14 }
};
export function CreateSchemaModel(props) {
  var formProps = props.formProps,
    schemaModelJSONCache = props.schemaModelJSONCache;
  var _a = useConfigContext(),
    schemaList = _a.schemaList,
    onGetSchemaModel = _a.onGetSchemaModel;
  var form = Form.useForm()[0];
  var schemaModelJSON = useRef('');
  var _b = useState(false),
    visible = _b[0],
    setVisible = _b[1];
  var _c = useState(false),
    visibleJSON = _c[0],
    setVisibleJSON = _c[1];
  var handleClick = function () {
    var _a = schemaModelJSONCache.current,
      name = _a.name,
      description = _a.description;
    form.setFieldsValue({ name: name, description: description });
    setVisible(true);
  };
  var handleSend = function () {
    var _a = schemaModelJSONCache.current,
      name = _a.name,
      description = _a.description;
    var values = { name: name, description: description };
    var schemaModel = __assign(__assign({}, values), {
      formProps: formProps,
      schemaList: schemaList
    });
    onGetSchemaModel(schemaModel);
    schemaModelJSON.current = JSON.stringify(schemaModel, null, 4);
    schemaModelJSONCache.current = schemaModelJSON.current;
    setVisible(false);
  };
  var handleCopy = function () {
    navigator.clipboard.writeText(schemaModelJSON.current);
    setVisibleJSON(false);
  };
  return React.createElement(
    React.Fragment,
    null,
    React.createElement(
      Button,
      {
        type: 'primary',
        disabled: !schemaList.length,
        onClick: function () {
          handleSend();
        }
      },
      '\u751F\u6210\u6A21\u677F'
    ),
    React.createElement(
      Modal,
      {
        title: '\u751F\u6210\u5E76\u53D1\u9001\u6A21\u677F',
        visible: visible,
        onOk: function () {
          return form.submit();
        },
        onCancel: function () {
          return setVisible(false);
        },
        getContainer: false
      },
      React.createElement(
        Form,
        __assign({ form: form }, layout, { onFinish: handleSend }),
        React.createElement(
          Form.Item,
          {
            label: '\u6A21\u677F\u540D\u79F0',
            name: 'name',
            rules: [{ required: true, message: '请填写模板名称！' }]
          },
          React.createElement(Input, null)
        ),
        React.createElement(
          Form.Item,
          { label: '\u63CF\u8FF0', name: 'description' },
          React.createElement(Input.TextArea, null)
        )
      )
    ),
    React.createElement(
      Modal,
      {
        title: 'schemaModel',
        width: '50vw',
        bodyStyle: { height: '60vh', overflow: 'auto' },
        visible: visibleJSON,
        onOk: handleCopy,
        okText: '\u590D\u5236',
        onCancel: function () {
          return setVisibleJSON(false);
        }
      },
      React.createElement('pre', null, schemaModelJSON.current)
    )
  );
}
